﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using TravelSite.DAL;
using TravelSite.DAL.Repositories;
using TravelSite.Entities;
using TravelSite.Code;

namespace TravelSite.Admin.References
{
    public partial class VacationCompanyPage : BaseDataPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public override void LoadData()
        {
            using (var repo = RepositoryFactory.ResolveRepository<VacationCompanyRepository>())
            {
                grid.DataSource = repo.FindAll();
                grid.DataBind();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void upDetails_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["__EVENTTARGET"]))
                if (Request["__EVENTTARGET"] != upDetails.ClientID)
                    return;
            
            if (!string.IsNullOrEmpty(Request["__EVENTARGUMENT"]))
            {
                hdId.Text = Request["__EVENTARGUMENT"];
                if (hdId.Text != "-1")
                {
                    using (var repo = RepositoryFactory.ResolveRepository<VacationCompanyRepository>())
                    {
                        lblDialogTitle.Text = "Edit Vacation Company";
                        VacationCompany item = repo.Find(Convert.ToInt32(hdId.Text));
                        txtVacationCompanyName.Text = item.VacationCompanyName;
                        chbIsDefault.Checked = item.IsDefault;
                    }
                }
                else
                {
                    lblDialogTitle.Text = "Add New Vacation Company";
                    txtVacationCompanyName.Text = "";
                    chbIsDefault.Checked = false;
                }
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ShowEdit", "$('#divDialog').dialog('open');", true);
            }
        }

        protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grid.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<VacationCompanyRepository>())
            {
                VacationCompany itm;

                if (hdId.Text == "-1")
                {
                    itm = new VacationCompany();
                    AssignValues(itm);
                    repo.Insert(itm);
                }
                else
                {
                    itm = repo.Find(Convert.ToInt32(hdId.Text));
                    AssignValues(itm);
                    repo.Update(itm);
                    /*if (!string.IsNullOrEmpty(result))
                    {
                        JsCodeToRunOnReady = string.Format("$('#divDialog').dialog('open'); alert('{0}');", result);
                        return;
                    }*/
                }
            }

            LoadData();
            upMain.Update();
        }

        void AssignValues(VacationCompany itm)
        {
            itm.VacationCompanyName = txtVacationCompanyName.Text;
            itm.IsDefault = chbIsDefault.Checked;
        }

        protected void grid_RowCommand(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());

            using (var repo = RepositoryFactory.ResolveRepository<VacationCompanyRepository>())
            {
                switch (e.CommandName)
                {
                    case "delete":
                        repo.Delete(id);
                        break;
                }
            }
            LoadData();
        }

        protected void grid_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                VacationCompany item = (VacationCompany)e.Row.DataItem;

                ImageButton ibtn = (ImageButton)e.Row.FindControl("lbtnDelete");
                ibtn.Visible = item.CanDelete;
                if (item.IsDefault)
                {
                    ibtn.Visible = false;
                }
            }
        }        
    }
}